import fs from 'node:fs';
import path from 'node:path';

const PARENT_DIR = String.raw`\\10.1.1.5\video\_待处理\镇魂街`;

// 解析命令行参数
const [subDirName, seasonArg] = process.argv.slice(2);

if (!subDirName) {
    console.error('Error: Missing required subdirectory name argument');
    process.exit(1);
}

// 验证并处理第二个参数
let seasonCount = 1;
if (seasonArg) {
    const parsed = Number(seasonArg);
    if (!Number.isInteger(parsed) || parsed < 1 || parsed > 99) {
        console.error('Error: Second argument must be an integer between 1-99');
        process.exit(1);
    }
    seasonCount = parsed;
}

// 创建主目录
const targetDir = path.join(PARENT_DIR, subDirName);
try {
    if (!fs.existsSync(targetDir)) {
        fs.mkdirSync(targetDir);
        console.log(`Created main directory: ${subDirName}`);
    }
} catch (error) {
    console.error(`Error creating main directory: ${error.message}`);
    process.exit(1);
}

// 创建Season目录
try {
    for (let i = 1; i <= seasonCount; i++) {
        const seasonNum = i.toString().padStart(2, '0');
        const seasonDir = path.join(targetDir, `Season ${seasonNum}`);

        if (!fs.existsSync(seasonDir)) {
            fs.mkdirSync(seasonDir);
            console.log(`Created season directory: Season ${seasonNum}`);
        }
    }
} catch (error) {
    console.error(`Error creating season directories: ${error.message}`);
    process.exit(1);
}

console.log('Operation completed successfully');
